﻿using AutoMapper;
using BW.Demo.Domain.Entities;
using BW.Demo.ErrorCode;
using BW.Demo.Infrastructure.Interfcaes;
using BW.Demo.WebApi.Applications.Commands;
using BW.Demo.WebApi.Applications.Commands.Deputy;
using MediatR;

namespace BW.Demo.WebApi.Applications.CommandHandlers.Deputy
{
   
    public class PicUpdateCommandHandler : IRequestHandler<PicUpdateCommand, APIResult<int>>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRespitory<PictureInformation> _baseRespitory;
        public PicUpdateCommandHandler(IMapper mapper, IBaseRespitory<PictureInformation> baseRespitory)
        {
            _mapper = mapper;
            _baseRespitory = baseRespitory;
        }
        public async Task<APIResult<int>> Handle(PicUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            PictureInformation trace = _mapper.Map<PictureInformation>(request);
            int num = await _baseRespitory.UpdateAsync(trace);

            if (num > 0)
            {
                result.Code = ResultCode.更新成功;
                result.Message = ResultCode.更新成功.ToString();
                result.Data = num;

            }
            else
            {

                result.Code = ResultCode.更新失败;
                result.Message = ResultCode.更新失败.ToString();
                result.Data = num;
            }

            return await Task.FromResult(result);
        }
    }
}
